//
//  ViewController.m
//  01-loginAndRegister
//
//  Created by panxubin on 16/8/24.
//  Copyright © 2016年 panxubin. All rights reserved.
//

#import "LoginViewController.h"
#import "UIView+HM.h"
#import "UITextField+placeHolder.h"
#import "HMLable.h"
#import "searchPsd.h"
#import <Masonry.h>
#define screenSize [UIScreen mainScreen].bounds.size

@interface LoginViewController ()

@property(strong,nonatomic)UIButton *haveNameBtn;
@property(strong,nonatomic)UIView *textFiliedView;

@property(strong,nonatomic)UIButton *loginBtn;
@property(strong,nonatomic)UIButton *registerBtn;

@property(strong,nonatomic)UIView *loginView;
@property(strong,nonatomic)UIView *registerView;
@property(strong,nonatomic)UIButton *disMissBtn;

@property(strong,nonatomic)UIImageView *leftImageView;
@property(strong,nonatomic)UIImageView *rightImageView;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addBackImage];
    [self addUITextField];
    [self addLineView];
    
//   隐藏导航栏和底部tabbar栏
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}

#pragma mark - 添加背景毛玻璃
-(void)addBackImage{
    //    背景毛玻璃
    UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    bgImageView.image = [UIImage imageNamed:@"login_register_background_320x568_"];

    bgImageView.userInteractionEnabled = YES;
    [self.view addSubview:bgImageView];
}

#pragma mark - 添加分割线的方法
-(void)addLineView{
    
    for (int i = 0; i<2; i++) {
        
        //    添加快速登录的分界线
        UIImage *lineImage ;
        if (i == 0) {
            lineImage = [UIImage imageNamed:@"login_register_left_line_103x0_"];
        }else if (i == 1){
            lineImage = [UIImage imageNamed:@"login_register_right_line_103x0_"];
        }
        
        UIImageView *lineView = [[UIImageView alloc]initWithImage:lineImage];
        [self.view addSubview:lineView];
        [lineView makeConstraints:^(MASConstraintMaker *make) {
            
            if (i == 0) {
                make.right.equalTo(self.view.right).offset(- (screenSize.width / 2 + 30));
                make.left.equalTo(self.view.left).offset(8);
                self.leftImageView = lineView;
            }else if (i == 1){
                
                make.left.equalTo(self.view.left).offset((screenSize.width / 2 + 30));
                make.right.equalTo(self.view.right).offset(8);
                self.rightImageView = lineView;
            }
            
            make.top.equalTo(self.view.bottom).offset = -160;
            make.height.offset(lineImage.size.height);
        }];
    }
    
    UILabel *lable = [[UILabel alloc]init];
    lable.h = 80;
    lable.font = [UIFont systemFontOfSize:13];
    lable.text = @"快速登录";
    lable.textAlignment = NSTextAlignmentCenter;
    [lable setTextColor:[UIColor whiteColor]];
    [self.view addSubview:lable];
    [lable makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.leftImageView.right).offset(4);
        make.right.equalTo(self.rightImageView.left).offset(0);
        make.bottom.equalTo(self.view.bottom).offset(-160+ (lable.h/2));
        make.height.offset(lable.h);
        
    }];
    
//    添加三个按钮
    UIButton *qqBtn = [[UIButton alloc]init];
    [qqBtn setImage:[UIImage imageNamed:@"login_QQ_icon_70x70_"] forState:UIControlStateNormal];
    
    UIButton *sinlanBtn = [[UIButton alloc]init];
    [sinlanBtn setImage:[UIImage imageNamed:@"login_sina_icon_70x70_" ] forState:UIControlStateNormal];
    UIButton *tenxunwbBtn = [[UIButton alloc]init];
    [tenxunwbBtn setImage:[UIImage imageNamed:@"login_tecent_icon_70x70_"] forState:UIControlStateNormal];
    
    [self.view addSubview:qqBtn];
    [self.view addSubview:sinlanBtn];
    [self.view addSubview:tenxunwbBtn];
//    三个按钮添加约束
    [sinlanBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(lable.bottom).offset(0);
    }];
    [qqBtn makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(sinlanBtn.top).offset(0);
        make.right.equalTo(sinlanBtn.left).offset(-32);
    }];
    [tenxunwbBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(sinlanBtn.top).offset(0);
        make.left.equalTo(sinlanBtn.right).offset(32);
    }];
//    label
    HMLable *qqLable = [[HMLable alloc]init];
    qqLable.text = @"qq登录";
    [self.view addSubview:qqLable];
    
    HMLable *sinlanLab = [[HMLable alloc]init];
    sinlanLab.text = @"微博登录";
    [self.view addSubview:sinlanLab];
    
    HMLable *tenxunWBLab = [[HMLable alloc]init];
    tenxunWBLab.text = @"腾讯微博";
    [self.view addSubview:tenxunWBLab];
    
    [qqLable makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(qqBtn);
        make.top.equalTo(qqBtn.bottom).offset(8);
    }];
    
    [sinlanLab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(sinlanBtn);
        make.top.equalTo(sinlanBtn.bottom).offset(8);
    }];
    [tenxunWBLab makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(tenxunwbBtn);
        make.top.equalTo(tenxunwbBtn.bottom).offset(8);
    }];
    
}

//添加登录注册的文本框
-(void)addUITextField
{
//    添加登录的输入框
    NSArray *loginStrArr = @[@"手机号",@"密码",@"登 录"];
    [self addTextFiledWithNum:0 andtextArr:loginStrArr];
    
//    添加注册的输入框
    NSArray *registerStrArr = @[@"请输入手机号",@"包含大小写字母数字，长度不小于8位",@"注 册"];
    [self addTextFiledWithNum:1 andtextArr:registerStrArr];
    
//    添加忘记密码按钮
    UIButton *disMissPsw = [[UIButton alloc]init];
    self.disMissBtn = disMissPsw;
    [disMissPsw setTitle:@"忘记密码？" forState:UIControlStateNormal];
    disMissPsw.titleLabel.font = [UIFont systemFontOfSize:15];
    [disMissPsw setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:disMissPsw];
//    忘记密码按钮添加点击事件
    [disMissPsw addTarget:self action:@selector(gotoSearchPsd) forControlEvents:UIControlEventTouchUpInside];
    
    [disMissPsw makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginBtn.bottom).offset(16);
        make.right.equalTo(self.loginBtn.right).offset(0);
        
    }];
    
//    添加返回按钮
    UIButton *cancelBtn = [[UIButton alloc]init];
    [cancelBtn setTitle:@"不注册了" forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:cancelBtn];
//    不注册了，添加点击事件
    [cancelBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    [cancelBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.top).offset(32);
        make.left.equalTo(self.view.left).offset(8);
    }];
    
    //    添加已经有账号了按钮
    UIButton *haveNameBtn = [[UIButton alloc]init];
    self.haveNameBtn = haveNameBtn;
//    添加点击事件
    [haveNameBtn addTarget:self action:@selector(replace) forControlEvents:UIControlEventTouchUpInside];
    [haveNameBtn setTitle:@"注册账号" forState:UIControlStateNormal];
    [haveNameBtn setTitle:@"已有账号？" forState:UIControlStateSelected];
    haveNameBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [haveNameBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:haveNameBtn];
    
    [haveNameBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view.top).offset(32);
        make.right.equalTo(self.view.right).offset(-8);
    }];
}

#pragma mark - 点击了忘记密码后的响应事件
-(void)gotoSearchPsd
{
//    创建控制器，跳转到对应的界面
    self.navigationController.navigationBar.hidden = NO;
    searchPsd *searchVC = [[searchPsd alloc]init];
    searchVC.title = @"找回密码";
    [self.navigationController pushViewController:searchVC animated:YES];
}

#pragma mark - 点击了不注册按钮的响应事件
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 添加文本框方法
//封装添加登录文本框的方法
-(void)addTextFiledWithNum:(int)num andtextArr:(NSArray *)arr{

    UIView *backView = [[UIView alloc]init];

    backView.backgroundColor = [UIColor colorWithRed:203 / 255.0 green:203 / 255.0 blue:203 / 255.0 alpha:0.18];
    [self.view addSubview:backView];
    self.textFiliedView = backView;
    
    NSInteger y = ([UIScreen mainScreen].bounds.size.height - (58 + 116 + 32)) * (1 - 0.618);
    CGFloat x = (num * [UIScreen mainScreen].bounds.size.width) + 32;

    CGFloat width = [UIScreen mainScreen].bounds.size.width - 2 * 32;
    CGFloat height = 116;
    backView.frame = CGRectMake(x, y, width, height);
    //
    backView.layer.masksToBounds = YES;
    backView.layer.cornerRadius = 8;
    backView.layer.borderWidth = 0.5f;
    UIColor *lineColor = [UIColor colorWithRed:239 / 255.0 green:239 / 255.0 blue:239 / 255.0 alpha:0.5];
    backView.layer.borderColor = [lineColor CGColor];
    
//    //    添加一个图片作为背景图片
//    UIImageView *bgImageView = [[UIImageView alloc]init];
//    bgImageView.image = [UIImage imageNamed:@"login_rgister_textfield_bg_266x92_"];
//    bgImageView.frame = backView.frame;
//    [backView addSubview:bgImageView];
    
//    添加中间的分割线
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor colorWithRed:239 / 255.0 green:239 / 255.0 blue:239 / 255.0 alpha:0.3];;
    [backView addSubview:lineView];
    
//    添加约束
    [lineView makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.equalTo(backView).offset(0);
        make.top.equalTo(backView.top).offset(57.5);
        make.height.offset(1);
    }];
    
    //    中间输入的文本框
    UITextField *nameText = [[UITextField alloc]init];
    UITextField *psdText = [[UITextField alloc]init];
    
    [backView addSubview:nameText];
    [backView addSubview:psdText];
    
    [nameText mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(backView.top).offset(0);
        make.left.equalTo(backView.left).offset(4);
        make.right.equalTo(backView.right).offset(-4);
        make.height.offset(58);
    }];
    
    [psdText makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(nameText.bottom).offset(0);
        make.left.equalTo(backView.left).offset(4);
        make.right.equalTo(backView.right).offset(-4);
        make.height.offset(58);
    }];
    
    //    提示文字
    NSString *nameStr = arr[0];
    NSString *pswStr = arr[1];
//    增加按钮，将按钮
    
    nameText.placeholder = nameStr;
    psdText.placeholder = pswStr;
    [nameText becomeFirstResponder];
    [psdText becomeFirstResponder];
    nameText.textColor = [UIColor whiteColor];
    psdText.textColor = [UIColor whiteColor];
    nameText.colorNum = @"哈哈哈";
    psdText.colorNum = @"哈哈哈哈";
    
    nameText.borderStyle = UITextBorderStyleNone;
    psdText.borderStyle = UITextBorderStyleNone;
    
    //    添加按钮
    UIButton *loginbtn = [[UIButton alloc]init];
    [loginbtn setBackgroundImage:[UIImage imageNamed:@"login_register_button_click_87x40_@1x"] forState:UIControlStateNormal];
    NSString *loginBtnStr = arr[2];
    [loginbtn setTitle:loginBtnStr forState:UIControlStateNormal];
    [loginbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginbtn.layer.masksToBounds = YES;
    loginbtn.layer.cornerRadius = 8;
    
    if (num == 0) {
        self.loginBtn = loginbtn;
    }else{
        self.registerBtn = loginbtn;
    }
    
    if (num == 0) {
        self.loginView = backView;
    }else{
        self.registerView = backView;
    }
    
    [self.view addSubview:loginbtn];
    [loginbtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(backView.bottom).offset(32);
        make.left.equalTo(backView.left).offset(0);
        make.right.equalTo(backView.right).offset(0);
        make.height.offset(58);
    }];
}

#pragma mark - 切换登录和注册框
//切换登录和注册框
-(void)replace{

    NSLog(@"切换登录注册");
    CGFloat length = [UIScreen mainScreen].bounds.size.width;
    if (self.haveNameBtn.selected) {
        
        [UIView animateWithDuration:0.2 animations:^{
            self.loginView.x += length;
            self.loginBtn.x += length;
            self.registerView.x += length;
            self.registerBtn.x += length;
        } completion:^(BOOL finished) {
            self.disMissBtn.hidden = !self.disMissBtn.hidden;
        }];
        
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            self.loginView.x -= length;
            self.loginBtn.x -= length;
            self.registerView.x -= length;
            self.registerBtn.x -= length;
        }];
        self.disMissBtn.hidden = !self.disMissBtn.hidden;
    }
    self.haveNameBtn.selected = !self.haveNameBtn.selected;
}

@end











